<template>
  <div class="box6">
    <div class="title">
      <p>机器人状态</p>
      <img src="../../images/dataScreen-title.png" alt="" />
    </div>
    <!-- 图形图标的容器 -->
    <div class="charts" ref="charts"></div>
  </div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
import { ref, onMounted } from "vue";
import { RobotApi } from "@/api/map/statelist";
//获取DOM节点
let charts = ref();
//组件挂载完毕
onMounted(async () => {
  //一个容器可以同时展示多种类型的图形图标
  let mychart = echarts.init(charts.value);
  const res = await RobotApi.getRobotStateList();

  //console.log(res, "getRobotStateList");
  // 动态生成 series
  const series = [
    {
      name: "在线",
      type: "bar",
      data: [],
      barWidth: "15%",
      itemStyle: { color: "#4FC09B" },
    },
    {
      name: "离线",
      type: "bar",
      data: [],
      barWidth: "15%",
      itemStyle: { color: "#B9BFC5" },
    },
    {
      name: "故障",
      type: "bar",
      data: [],
      barWidth: "15%",
      itemStyle: { color: "#FE556A" },
    },
  ];

  res.forEach((state: any) => {
    if (state.name === "在线") {
      series[0].data.push(state[1], state[2], state[3]);
    } else if (state.name === "离线") {
      series[1].data.push(state[1], state[2], state[3]);
    } else if (state.name === "故障") {
      series[2].data.push(state[1], state[2], state[3]);
    }
  });
  // 设置图表配置
  var option = {
    title: {
      top: 8,
      left: 8,
      text: "当前机器人状态",
      textStyle: {
        fontSize: 14,
        color: "#ffffff", // 设置标题文字为白色
      },
      // subtext: '每个机器人分别展示在线、离线和故障状态'
    },
    tooltip: {
      trigger: "axis", // 触发方式是坐标轴
      axisPointer: {
        type: "none", // 使用阴影效果
      },
      backgroundColor: "transparent", // 设置背景透明
      borderColor: "rgba(41, 252, 255, 0.3)", // 使用较浅的边框颜色
      borderWidth: 2, // 设置边框宽度
      textStyle: {
        color: "#fff", // 设置文字颜色
        fontSize: 14, // 设置字体大小
      },
      padding: [5, 10], // 设置 tooltip 内边距
    },
    legend: {
      data: ["在线", "离线", "故障"],
      top: 50,
      left: 12,
      itemWidth: 8, // 设置图例标识符的宽度
      itemHeight: 8, // 设置图例标识符的高度
      textStyle: {
        color: "#ffffff", // 设置
        fontSize: 8, // 设置字体大小标题文字为白色
      },
    },
    grid: {
      top: "30%",
      left: "6%",
      right: "0",
      bottom: "6%",
      containLabel: true,
    },
    xAxis: {
      type: "category",
      data: ["智能盘点机器人", "智能还书箱系统", "智能寻览机器人"], // x轴数据为智能盘点机器人，2，3
      axisLabel: {
        interval: 0, // 保证所有x轴标签都显示
        color: "#ffffff", // 设置x轴标签字体为白色
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false, // 隐藏x轴线条
      },
    },
    yAxis: {
      type: "value",
      interval: 10, // 设置刻度间隔为 1
      axisLabel: {
        formatter: "{value}", // 格式化y轴显示
        color: "#ffffff", // 设置y轴标签字体为白色
      },
      splitLine: {
        lineStyle: {
          color: "#000000", // 设置y轴的横向分隔线为黑色
        },
      },
    },

    series: series, // 使用动态生成的 series
  };

  mychart.setOption(option);
});

// const getstatelist = async () => {
//   const res = await RobotApi.getRobotStateList();
//   statelist.value = res;
//   console.log(statelist.value, "res");
// };
</script>

<style scoped lang="scss">
.box6 {
  width: 100%;
  height: 350px;
  background: url(../../images/dataScreen-main-lt.png) no-repeat;
  background-size: 100% 100%;
  margin-top: 10px;
  margin-bottom: 10px;

  .title {
    margin-left: 5px;

    p {
      color: white;
      font-size: 20px;
    }
  }

  .charts {
    // background-color: aqua;
    border: 1px solid #516171;
    margin-top: 3px;
    margin-bottom: 6px;
    margin-left: 38px;

    width: 460px;
    height: 285px;
  }
}
</style>
